package cn.com.thingo.hyintelligent.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.v7.app.AppCompatDelegate;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.ezvizuikit.open.EZUIKit;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.tencent.bugly.crashreport.CrashReport;
import com.th.opensdk.openapi.THOpenSDK;
import com.videogo.openapi.EZOpenSDK;

import java.util.HashSet;
import java.util.Set;

import cn.com.thingo.hyintelligent.BuildConfig;
import cn.com.thingo.hyintelligent.di.component.AppComponent;
import cn.com.thingo.hyintelligent.di.component.DaggerAppComponent;
import cn.com.thingo.hyintelligent.di.module.AppModule;
import cn.com.thingo.hyintelligent.di.module.HttpModule;
import cn.com.thingo.hyintelligent.model.bean.GatewayListBean;
import cn.com.thingo.hyintelligent.model.bean.LoginBean;
import cn.jpush.android.api.JPushInterface;


public class App extends Application {
    private static LoginBean.DataBean loginDataBean = null;
    private static GatewayListBean.DataBean dataBean;
    public static String EZ_APP_Key = "691d7760784f4354ba1a93a19ba5cd7e";
    public static String EZ_SECRET = "428b9db4946be0775b97aaa9f7f502a2";
    private boolean gatewayLoginStatus = true;
    private boolean scenesStatus = true;
    private long currentScenesId;
    private long currentAirConditionId;
    private int currentRoomId;
    private boolean select = true;

    public long getCurrentAirConditionId() {
        return currentAirConditionId;
    }

    public void setCurrentAirConditionId(long currentAirConditionId) {
        this.currentAirConditionId = currentAirConditionId;
    }
    public int getCurrentRoomId() {
        return currentRoomId;
    }

    public void setCurrentRoomId(int currentRoomId) {
        this.currentRoomId = currentRoomId;
    }

    public boolean isScenesStatus() {
        return scenesStatus;
    }

    public void setScenesStatus(boolean scenesStatus) {
        this.scenesStatus = scenesStatus;
    }

    public long getCurrentScenesId() {
        return currentScenesId;
    }

    public void setCurrentScenesId(long currentScenesId) {
        this.currentScenesId = currentScenesId;
    }

    public static GatewayListBean.DataBean getDataBean() {
        return dataBean;
    }

    public static void setDataBean(GatewayListBean.DataBean dataBean) {
        App.dataBean = dataBean;
    }

    private static App instance;
    public static AppComponent appComponent;
    private Set<Activity> allActivities;

    public static int SCREEN_WIDTH = -1;
    public static int SCREEN_HEIGHT = -1;
    public static float DIMEN_RATE = -1.0F;
    public static int DIMEN_DPI = -1;


    public Set<Activity> getAllActivities() {
        return allActivities;
    }

    public static synchronized App getInstance() {
        return instance;
    }

    public boolean isGatewayLoginStatus() {
        return gatewayLoginStatus;
    }

    public void setGatewayLoginStatus(boolean gatewayLoginStatus) {
        this.gatewayLoginStatus = gatewayLoginStatus;
    }

    static {
        AppCompatDelegate.setDefaultNightMode(
                AppCompatDelegate.MODE_NIGHT_NO);
    }

    public static LoginBean.DataBean getLoginDataBean() {
        return loginDataBean;
    }

    public static void setLoginDataBean(LoginBean.DataBean loginDataBean) {
        //todo 绑定推送
//        App.getInstance().setJpushAlias(loginDataBean.getMember_list_id() + "");
        App.loginDataBean = loginDataBean;
    }

    public void setJpushAlias(String alias) {
        JPushInterface.setAlias(this, 101, alias);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SystemParams.init(this);
        //初始化网关硬件
        THOpenSDK.initSdk(this);

        instance = this;

        JPushInterface.setDebugMode(false);//正式版的时候设置false，关闭调试
        JPushInterface.init(this);


        //bugly
        //萤石云初始化
        if (BuildConfig.isDebug) {
            CrashReport.initCrashReport(getApplicationContext(), "1e9fbeb3b1", true);
            EZOpenSDK.showSDKLog(true);
        } else {
            CrashReport.initCrashReport(getApplicationContext(), "1e9fbeb3b1", false);
            EZOpenSDK.showSDKLog(false);
        }
        EZOpenSDK.enableP2P(false);
        EZOpenSDK.initLib(this, EZ_APP_Key);
        //萤石ui组件初始化
        EZUIKit.initWithAppKey(this, EZ_APP_Key);
//        MobSDK.init(this);

        //初始化屏幕宽高
        getScreenSize();

        Logger.addLogAdapter(new AndroidLogAdapter());

        //初始化数据库
        //Realm.init(getApplicationContext());

        //在子线程中完成其他初始化
        //InitializeService.start(this);
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    public void exitApp() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    act.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    public void getScreenSize() {
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        Display display = windowManager.getDefaultDisplay();
        display.getMetrics(dm);
        DIMEN_RATE = dm.density / 1.0F;
        DIMEN_DPI = dm.densityDpi;
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        if (SCREEN_WIDTH > SCREEN_HEIGHT) {
            int t = SCREEN_HEIGHT;
            SCREEN_HEIGHT = SCREEN_WIDTH;
            SCREEN_WIDTH = t;
        }
    }

    public static AppComponent getAppComponent() {
        if (appComponent == null) {
            appComponent = DaggerAppComponent.builder()
                    .appModule(new AppModule(instance))
                    .httpModule(new HttpModule())
                    .build();
        }
        return appComponent;
    }

    public void setSelect(boolean b) {
        this.select = b;
    }

    public boolean isSelect() {
        return select;
    }
}
